import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const router = new VueRouter({
    mode: 'history',
    routes: [
        {
            path: '/',
            name: 'Login',
            component: () => import('../views/Login'),
            meta: {
                title: "电子报告智能化管理平台"
            }
        },
        {
            path: '/logon',
            name: 'logon',
            component: () => import('../views/logon'),
            meta: {
                title: "电子报告智能化管理平台"
            }
        },
        {
            path: '/studentCode',
            name: 'studentCode',
            component: () => import('../views/studentCode'),
            meta: {
                title: "电子报告智能化管理平台"
            }
        },
        {
            path: '/home',
            name: 'home',
            component: () => import('../views/Home'),
            meta: {
                title: "电子报告智能化管理平台"
            },
            children: [
                {path: '/report', name: 'report', component: () => import("../pages/student/report")},
                {path: '/prescription', name: 'prescription', component: () => import("../pages/student/prescription")},
                {path: '/setting', name: 'setting', component: () => import("../pages/student/setting")},
                {path: '/template', name: 'template', component: () => import("../pages/student/template")},
                {path: '/notice', name: 'notice', component: () => import("../pages/student/notice")},
                {path: '/userCenter', name: 'userCenter', component: () => import("../pages/student/userCenter")},
                {
                    path: '/changePassword',
                    name: 'changePassword',
                    component: () => import("../pages/student/changePassword")
                },
                {
                    path: '/recipeManagement',
                    name: 'recipeManagement',
                    component: () => import("../pages/teacher/recipeManagement")
                },
                {
                    path: '/reportManagement',
                    name: 'reportManagement',
                    component: () => import("../pages/teacher/reportManagement")
                },
                {
                    path: '/templateManagement',
                    name: 'templateManagement',
                    component: () => import("../pages/teacher/templateManagement")
                },
                {path: '/studentCase', name: 'studentCase', component: () => import("../pages/teacher/studentCase")},
                {path: '/createNotice', name: 'createNotice', component: () => import("../pages/teacher/createNotice")},
                {path: '/laboratory', name: 'laboratory', component: () => import("../pages/teacher/laboratory")},
                {path: '/classes', name: 'classes', component: () => import("../pages/teacher/classes")},
                {path: '/caseCategory', name: 'caseCategory', component: () => import("../pages/teacher/caseCategory")},
                {
                    path: '/userManagement',
                    name: 'userManagement',
                    component: () => import("../pages/admin/userManagement")
                },
                {
                    path: '/laboratoryManagement',
                    name: 'laboratoryManagement',
                    component: () => import("../pages/admin/laboratoryManagement")
                },
                {path: '/log', name: 'log', component: () => import("../pages/admin/log")},

            ]
        }
    ]

})
// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
    if (to.path === '/' || to.path === '/logon' || to.path === '/studentCode') {
        next()
    } else {
        const token = window.sessionStorage.getItem('token')
        if (!token) {
            next('/')
        } else {
            next()
        }
    }
})

export default router
